﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class DoiMatKhau : Form
    {
        public DoiMatKhau()
        {
            InitializeComponent();
        }

        private void xoaThongTin()
        {
            txtMatKhauHienTai.Text = null;
            txtMatKhauMoi.Text = null;
            txtNhapLaiMatKhauMoi.Text = null;
        }

        private void doiMatKhau(string matKhauCu, string matKhauMoi, string NhapLaiMatKhauMoi)
        {

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from NhanVien where MatKhau_NV = @MatKhau_NV";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MatKhau_NV", matKhauCu);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                if (matKhauMoi == NhapLaiMatKhauMoi)
                {
                    reader.Close();
                    cmd.CommandText = "update NhanVien set MatKhau_NV = @MatKhau_NV";
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@MatKhau_NV", matKhauMoi);
                    cmd.ExecuteNonQuery();
                    LblThongBao.Text = "Thay đổi mật khẩu thành công";
                    conn.Close();
                }
                else
                {
                    reader.Close();
                    LblNhapLaiMatKhauMoiError.Text = "Mật khẩu mới không đúng";
                    conn.Close();
                }
            }
            else
            {
                reader.Close();
                LblMatKhauCu.Text = "Mật khẩu cũ không đúng";
                conn.Close();
            }
        }

        private void butDongY_Click(object sender, EventArgs e)
        {
            doiMatKhau(txtMatKhauHienTai.Text, txtMatKhauMoi.Text, txtNhapLaiMatKhauMoi.Text);
            xoaThongTin();
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
